<?php
/**
 * lzp300 2015-01-27
 */
class common extends actionAbstract {


    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
    }

    function login(){
		acl::delCookie('qingMg_admininfo');
        //unset($_SESSION['qingMg_admininfo']);
        include $this->loadView();
    }
    
    /*
     * 验证登录信息
     */
    public function checkLogin(){
		$this->loadModel('system','account');
		$userName=isset($_POST['userName'])?$_POST['userName']:'';
		$password=isset($_POST['password'])?$_POST['password']:'';
		$yzmCode=isset($_POST['yzmCode'])?$_POST['yzmCode']:'';
		
		if(!$userName){
			exit(json_encode(array('state' => 0, 'html' =>  '请输入用户名')));
		}elseif(!$password){
			exit(json_encode(array('state' => 0, 'html' =>  '请输入密码')));
		}elseif(!$yzmCode){
			exit(json_encode(array('state' => 0, 'html' =>  '请输入验证码')));
		}elseif($yzmCode!=$_COOKIE['verifycode_content']){
			exit(json_encode(array('state' => 0, 'html' =>  '验证码错误')));
		}
		$password=md5($password);
		$sql="select * from system_account where username='".$userName."' and password='".$password."' and status=1";
		$re=$this->system->accountModel->fetchRow($sql);
		if(!$re){exit(json_encode(array('state' => 0, 'html' =>  '用户名或密码错误')));}
		if($re['status']!=1){exit(json_encode(array('state' => 0, 'html' =>  '账号被冻结，请联系管理员')));}
		//存储ip  最后时间
		$data['lastip']=$_SERVER["REMOTE_ADDR"];
		$data['lasttime']=time();
		$this->system->accountModel->update($data,$re['id']);
		//存储cookie session
		acl::setCookie("qingMg_admininfo",$re);
		//$_SESSION['qingMg_admininfo']=$re;
		$url=$this->url('entry/index');
		exit(json_encode(array('state' => 1, 'html' =>  '登录成功！','url'=>$url)));
    }
    /**
    * @desc 安全退出
    */
    public function logout(){
		acl::delCookie('qingMg_admininfo');
        //unset($_SESSION['qingMg_admininfo']);
		$url=$this->url('common/login');
		exit(json_encode(array('state' => 1, 'html' =>  '退出成功！','url'=>$url)));
    }

    /**
    * @desc 验证码
    */
    function yzmCode(){
        $this->loadHelper("verifyCode");
        $code = new verifyCode(50,26);
        echo $code;
    }
    
    /*
     * 2016-10-24 14:01:33
     * xuefei
     * ajax获取城市列表
     */
    function ajaxGetCityList(){
        $this->loadModel('qm','city');
        $this->loadModel('tc','city');
        $this->loadApi('hotelapi');
        $api = new hotelapi();
        $resource = $api->getCity();
        if($resource['status']!=1){
            exit(json_encode(array('state'=>0,'html'=>'更新失败，接口调用错误')));
        }else{
            $areOk=0;
            $data = array();
            foreach($resource['list']['city'] as $key => $value){
                if(strpos($value['@attributes']['name'], "市") || strpos($value['@attributes']['name'], "县")){
                    $len = mb_strlen($value['@attributes']['name'],'utf-8');
                    $name = mb_substr($value['@attributes']['name'],0,$len-1,'utf-8')."<br/>";
                }else{
                    $name = $value['@attributes']['name'];
                }
                $tc = $this->tc->cityModel->fetchRow("select * from tc_city where name like '%".$name."%'");
                if($tc){
                    $isset=$this->qm->cityModel->fetchRow("select * from qm_city where cid =".$value['@attributes']['id']);
                    $data['tcid'] = $tc['osiiid'];
                    $data['cid'] = $value['@attributes']['id'];
                    $data['name'] = $value['@attributes']['name'];
                    $data['suoxie'] = $value['@attributes']['suo-xie'];
                    $data['code'] = $value['@attributes']['code'];
                    if(strpos($value['@attributes']['pin-yin'], "'")){
                        $data['pinyin'] = str_replace("'", "''",$value['@attributes']['pin-yin']);
                    }else{
                        $data['pinyin'] = $value['@attributes']['pin-yin'];
                    }
                    $data['created'] = time();
                    if($isset){
                        $re=$this->qm->cityModel->update($data,$isset['id']);
                    }else{
                        $re=$this->qm->cityModel->insert($data);
                    }
                    if($re){$areOk+=1;}
                }
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个城市！")));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新城市！")));
            }
        }
    }
    
    /*
     * 2016-10-25 15:09:39
     * xuefei
     * ajax获取区域列表
     */
    function ajaxGetAreaList(){
        set_time_limit(0);
        $this->loadApi('hotelapi');
        $this->loadModel('qm','city');
        $this->loadModel('qm','area');
        $api = new hotelapi();
        $cityList = $this->qm->cityModel->fetchAll("select cid from qm_city");
        if(!empty($cityList)){
            $areOk=0;
            $data = array();
            foreach ($cityList as $key => $value){
                $resource = $api->getArea(array('cityid' => $value['cid']));
                if($resource['status']!=1){
                    $areOk .= 'failed';
                }else{
                    if(!empty($resource['list']['area'])){
                        foreach($resource['list']['area'] as $k => $v){
                            if(empty($v['@attributes']['id'])){
                                $data['aid'] = $resource['list']['area']['@attributes']['id'];
                                $data['name'] = $resource['list']['area']['@attributes']['name'];
                                $data['suoxie'] = $resource['list']['area']['@attributes']['suo-xie'];
                                //过滤单引号
                                if(strpos($resource['list']['area']['@attributes']['pin-yin'], "'")){
                                    $data['pinyin'] = str_replace("'", "''",$resource['list']['area']['@attributes']['pin-yin']);
                                }else{
                                    $data['pinyin'] = $resource['list']['area']['@attributes']['pin-yin'];
                                }
                                $data['cid'] = $value['cid'];
                                $data['created'] = time();
                            }else {
                                $data['aid'] = $v['@attributes']['id'];
                                $data['name'] = $v['@attributes']['name'];
                                $data['suoxie'] = $v['@attributes']['suo-xie'];
                                if(strpos($v['@attributes']['pin-yin'], "'")){
                                    $data['pinyin'] = str_replace("'", "''",$v['@attributes']['pin-yin']);
                                }else{
                                    $data['pinyin'] = $v['@attributes']['pin-yin'];
                                }
                                $data['cid'] = $value['cid'];
                                $data['created'] = time();
                            }
                            $isset=$this->qm->areaModel->fetchRow("select * from qm_area where aid =".$data['aid']);
                            if($isset){
                                $re=$this->qm->areaModel->update($data,$isset['id']);
                            }else{
                                $re=$this->qm->areaModel->insert($data);
                            }
                            if($re){$areOk+=1;}
                        }
                    }
                }
                
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个城市！")));
            }else if(strpos($areOk, "failed")){
                exit(json_encode(array('state'=>0,'html'=>'更新失败，接口调用错误')));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新城市！")));
            }
        }else{
            exit(json_encode(array('state'=>0,'html'=>"获取城市ID失败！")));
        }
    }
    
    /*
     * 2016-10-26 10:26:06
     * xuefei
     * ajax获取地标
     */
    function ajaxGetTagList(){
        set_time_limit(0);
        $this->loadApi('hotelapi');
        $this->loadModel('qm','city');
        $this->loadModel('qm','tag');
        $api = new hotelapi();
        $cityList = $this->qm->cityModel->fetchAll("select cid from qm_city");
        if(!empty($cityList)){
            $areOk=0;
            $data = array();
            foreach ($cityList as $key => $value){
                $resource = $api->getTag(array('cityid' => $value['cid']));
                if($resource['status']!=1){
                    $areOk .= 'failed';
                }else{
                    if(!empty($resource['list']['tag'])){
                        foreach($resource['list']['tag'] as $k => $v){
                            if(empty($v['@attributes']['id'])){
                                $data['tid'] = $resource['list']['tag']['@attributes']['id'];
                                $data['name'] = $resource['list']['tag']['@attributes']['name'];
                                $data['cid'] = $value['cid'];
                                $data['created'] = time();
                            }else {
                                $data['tid'] = $v['@attributes']['id'];
                                $data['name'] = $v['@attributes']['name'];
                                $data['cid'] = $value['cid'];
                                $data['created'] = time();
                            }
                            $isset=$this->qm->tagModel->fetchRow("select * from qm_tag where tid =".$data['tid']);
                            if($isset){
                                $re=$this->qm->tagModel->update($data,$isset['id']);
                            }else{
                                $re=$this->qm->tagModel->insert($data);
                            }
                            if($re){$areOk+=1;}
                        }
                    }
                    
                }
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个区域！")));
            }else if(strpos($areOk, "failed")){
                exit(json_encode(array('state'=>0,'html'=>'更新失败，接口调用错误')));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新区域！")));
            }
        }else{
            exit(json_encode(array('state'=>0,'html'=>"获取城市ID失败！")));
        }
    }
    
    /*
     * 2016-10-26 10:26:06
     * xuefei
     * ajax获取标签类型
     */
    function setTagType(){
        set_time_limit(0);
        $this->loadApi('hotelapi');
        $this->loadModel('qm','city');
        $this->loadModel('qm','tag');
        $api = new hotelapi();
        $type = $_POST['type'];
        $cityList = $this->qm->cityModel->fetchAll("select cid from qm_city");
        if(!empty($cityList)){
            $areOk=0;
            $data = array();
            foreach ($cityList as $key => $value){
                $resource = $api->getTag(array('cityid' => $value['cid'] , 'type' => $type));
                if($resource['status']!=1){
                    $areOk .= 'failed';
                }else{
                    if(!empty($resource['list']['tag'])){
                        foreach($resource['list']['tag'] as $k => $v){
                            if(empty($v['@attributes']['id'])){
                                $tid = $resource['list']['tag']['@attributes']['id'];
                            }else {
                                $tid = $v['@attributes']['id'];
                            }
                            $data['type'] = $type;
                            $isset=$this->qm->tagModel->fetchRow("select * from qm_tag where tid =".$tid);
                            if($isset){
                                $re=$this->qm->tagModel->update($data,$isset['id']);
                            }
                            if($re){$areOk+=1;}
                        }
                    }
                    
                }
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个地标！")));
            }else if(strpos($areOk, "failed")){
                exit(json_encode(array('state'=>0,'html'=>'更新失败，接口调用错误')));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新地标！")));
            }
        }else{
            exit(json_encode(array('state'=>0,'html'=>"获取城市ID失败！")));
        }
    }
    
    /*
     * 2016-11-9 14:51:35
     * xuefei
     * ajax获取酒店点评
     */
    function ajaxGetHotelComment(){
        set_time_limit(0);
        $this->loadApi('hotelapi');
        $this->loadModel('qm', 'comment');
        $api = new hotelapi();
        $hotel_id = empty($_POST['hid'])?'':$_POST['hid'];
        if(empty($_POST)){
            exit(json_encode(array('state'=>0,'html'=>'参数错误')));
        }
        $resource = $api->getHotelComment(array('hotelid' => $hotel_id));
        if($resource['status']!=1){
            exit(json_encode(array('state'=>0,'html'=>'获取失败，接口调用错误')));
        }else{
            $areOk=0;
            $data = array();
            if($resource['count'] > 0){
                if($resource['count'] > 50){
                    $pageindex = ceil($resource['count']/50);
                    for($i=1;$i<=$pageindex;$i++){
                        $comments = $api->getHotelComment(array('hotelid' => $hotel_id, 'pageindex' => $i));
                        foreach($comments['list']['comment'] as $key => $value){
                            $data['hid'] = $hotel_id;
                            $data['title'] = empty($value['title'])?'':$value['title'];
                            $data['user'] = empty($value['user'])?'':$value['user'];
                            $data['date'] = empty($value['date'])?'':$value['date'];
                            if(strpos($value['content'], "'")){
                                $data['content'] = str_replace("'", "''",$value['content']);
                            }else{
                                $data['content'] = $value['content'];
                            }
                            $data['G1'] = $value['@attributes']['g1'];
                            $data['G2'] = $value['@attributes']['g2'];
                            $data['G3'] = $value['@attributes']['g3'];
                            $data['G4'] = $value['@attributes']['g4'];
                            $data['G5'] = $value['@attributes']['g5'];
                            $data['G6'] = $value['@attributes']['g6'];
                            $data['istj'] = $value['istj'];
                            $data['orderId'] = $value['@attributes']['orderId'];
                            $data['created'] = time();
                            $isset=$this->qm->commentModel->fetchRow("select * from qm_comment where hid =".$hotel_id." and date ='".$data['date']."'");
                            if($isset){
                                $re=$this->qm->commentModel->update($data,$isset['id']);
                            }else{
                                $re=$this->qm->commentModel->insert($data);
                            }
                            if($re){$areOk+=1;}
                        }
                    }
                }else{
                    if($resource['count'] > 1){
                        foreach($resource['list']['comment'] as $key => $value){
                            $data['hid'] = $hotel_id;
                            $data['title'] = $value['title'];
                            $data['user'] = empty($value['user'])?'':$value['user'];
                            $data['date'] = $value['date'];
                            if(strpos($value['content'], "'")){
                                $data['content'] = str_replace("'", "''",$value['content']);
                            }else{
                                $data['content'] = $value['content'];
                            }
                            $data['G1'] = $value['@attributes']['g1'];
                            $data['G2'] = $value['@attributes']['g2'];
                            $data['G3'] = $value['@attributes']['g3'];
                            $data['G4'] = $value['@attributes']['g4'];
                            $data['G5'] = $value['@attributes']['g5'];
                            $data['G6'] = $value['@attributes']['g6'];
                            $data['istj'] = $value['istj'];
                            $data['orderId'] = $value['@attributes']['orderId'];
                            $data['created'] = time();
                            $isset=$this->qm->commentModel->fetchRow("select * from qm_comment where hid =".$hotel_id." and date ='".$data['date']."'");
                            if($isset){
                                $re=$this->qm->commentModel->update($data,$isset['id']);
                            }else{
                                $re=$this->qm->commentModel->insert($data);
                            }
                            if($re){$areOk+=1;}
                        }
                    }else{
                        foreach($resource['list'] as $key => $value){
                            $data['hid'] = $hotel_id;
                            $data['title'] = $value['title'];
                            $data['user'] = empty($value['user'])?'':$value['user'];
                            $data['date'] = $value['date'];
                            if(strpos($value['content'], "'")){
                                $data['content'] = str_replace("'", "''",$value['content']);
                            }else{
                                $data['content'] = $value['content'];
                            }
                            $data['G1'] = $value['@attributes']['g1'];
                            $data['G2'] = $value['@attributes']['g2'];
                            $data['G3'] = $value['@attributes']['g3'];
                            $data['G4'] = $value['@attributes']['g4'];
                            $data['G5'] = $value['@attributes']['g5'];
                            $data['G6'] = $value['@attributes']['g6'];
                            $data['istj'] = $value['istj'];
                            $data['orderId'] = $value['@attributes']['orderId'];
                            $data['created'] = time();
                            $isset=$this->qm->commentModel->fetchRow("select * from qm_comment where hid =".$hotel_id." and date ='".$data['date']."'");
                            if($isset){
                                $re=$this->qm->commentModel->update($data,$isset['id']);
                            }else{
                                $re=$this->qm->commentModel->insert($data);
                            }
                            if($re){$areOk+=1;}
                        }
                    }
                }
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个点评！")));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新点评！")));
            }
        }
    }
    
    
    /*
     * 2016-11-9 16:09:45
     * xuefei
     * ajax获取酒店交通
     */
    function ajaxGetHotelTraffic(){
        set_time_limit(0);
        $this->loadApi('hotelapi');
        $this->loadModel('qm', 'traffic');
        $api = new hotelapi();
        $hotel_id = empty($_POST['hid'])?'':$_POST['hid'];
        if(empty($_POST)){
            exit(json_encode(array('state'=>0,'html'=>'参数错误')));
        }
        $resource = $api->getHotelTraffic(array('hotelid' => $hotel_id));
        if($resource['status']!=1){
            exit(json_encode(array('state'=>0,'html'=>'获取失败，接口调用错误')));
        }else{
            $areOk=0;
            $data = array();
            if(!empty($resource['list'])){
                foreach($resource['list'] as $key => $value){
                    $data['hid'] = $hotel_id;
                    $data['htinfo'] = empty($value['htinfo'])?'':$value['htinfo'];
                    //过滤单引号
                    if(strpos($value['content'], "'")){
                        $data['content'] = str_replace("'", "''",$value['content']);
                    }else{
                        $data['content'] = $value['content'];
                    }
                    $data['created'] = time();
                    $isset=$this->qm->trafficModel->fetchRow("select * from qm_traffic where hid =".$hotel_id);
                    if($isset){
                        $re=$this->qm->trafficModel->update($data,$isset['id']);
                    }else{
                        $re=$this->qm->trafficModel->insert($data);
                    }
                    if($re){$areOk+=1;}
                }
            }
            if($areOk>0){
                exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个交通信息！")));
            }else{
                exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新交通信息！")));
            }
        }
    }
    
    
    //生成订单
    function ajaxBooking(){
        global $myconfigs;
        $this->loadModel('user', 'orders');
        $this->loadApi('hotelapi');
        $api = new hotelapi();
//        $order_id = empty($_POST['orderid'])?'':$_POST['orderid'];
        $order_id = 81439616699;
        
        $order = $this->user->ordersModel->fetchRow("select * from user_orders where orderid =".$order_id);
        if(!$order){
            $params = array();
            exit(json_encode(array('state'=>0,'html'=>"订单查询失败！")));
        }else{
            $b_params = array(
                'agentid' => $myconfigs['agentid'],
                'hotelid' => $order['hotelId'],
                'sdate' => date("Y-m-d",$order['starttime']),
                'edate' => date("Y-m-d",$order['stoptime']),
                'lastarrtime' => 24,
                'mobile' => $order['phone'],
                'contactor' => $order['name'],
                'gender' => 'M',
                'roominfo' => $order['roomId'].'||'.$order['roomnumber'],
                'orderid' => $order['orderid']
            );
            
            ksort($b_params);
            $book = $api->BookingOrder($b_params);
//            $params = array(
//                'hotelid' => $order['hotelId'],
//                'edate' => date("Y-m-d",$order['stoptime']),
//                'sdate' => date("Y-m-d",$order['starttime']),
//                'roominfo' => $order['roomId'].'||'.$order['roomnumber'],
//            );
//            ksort($params);
//            $re = $api->getOrderPrice($params);
//            if($re['status']!=1){
//                exit(json_encode(array('state'=>0,'html'=>"订单价格查询失败！")));
//            }else{
//                if($re['ysprice'] != $order['price']){
//                    exit(json_encode(array('state'=>0,'html'=>"订单金额不匹配！")));
//                }else{
//                    
//                }
//            }
        }
    }
    
}